package com.zpark.config;

import com.zpark.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author xiaoyu
 * @version 1.0 2022/9/20
 *
 * 拦截器
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //配置拦截规则
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建拦截器对象
        HandlerInterceptor interceptor = new LoginInterceptor();
        //注册拦截器
        InterceptorRegistration registration = registry.addInterceptor(interceptor);
        //配置拦截路径黑名单
        List<String> blackList = new ArrayList<>();
        blackList.add("/**"); // 拦截所有请求路径
        //配置不拦截的路径
        List<String> whiteList = new ArrayList<>();
        whiteList.add("/login.html");
        //添加拦截器
        registration.addPathPatterns(blackList);
        registration.excludePathPatterns(whiteList);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //当访问 localhost:8080/ 的时候默认跳转到 login.html 页面
        registry.addViewController("/").setViewName("/login.html");
        registry.addViewController("/index").setViewName("/login.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/zy/**").addResourceLocations("classpath:/zy/");
    }
}














